package com.test.vue.controller;

import com.test.vue.entity.CheckUserDate;
import com.test.vue.entity.QueryPlaceEntity.SysAreas;
import com.test.vue.entity.QueryPlaceEntity.SysCities;
import com.test.vue.entity.QueryPlaceEntity.SysProvinces;
import com.test.vue.service.QueryPlaceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by jack on 2017/10/30.
 */
@RestController
public class UploadController {
    private static final Logger logger = LoggerFactory.getLogger(UploadController.class);

    @Value("${imagesPath}")
    private String imagesPath;
    @Autowired
    private QueryPlaceService queryPlaceService;

    @RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
    public CheckUserDate uploadImage(@RequestParam(value = "file") MultipartFile file) throws RuntimeException {
        if (file.isEmpty()) {
            return CheckUserDate.build(400, "文件名不能为空");
        }
        // 获取文件名
        String fileName = file.getOriginalFilename();
        logger.info("上传的文件名为：" + fileName);
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //生成新的文件名
        String newImgName = new Date().getTime()+suffixName;
        logger.info("上传的后缀名为：" + suffixName);
        // 文件上传后的路径
        String filePath = imagesPath;
        // 解决中文问题，liunx下中文路径，图片显示问题
        // fileName = UUID.randomUUID() + suffixName;
        File dest = new File(filePath + newImgName);
        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
            System.out.println("创建成功");
        }
        try {
            file.transferTo(dest);
            logger.info("上传成功后的文件路径未：" + filePath + newImgName);
            return CheckUserDate.build(200, "图片:" + newImgName + ",上传成功");
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return CheckUserDate.build(400, "图片上传失败");
    }

    //在页面上展示图片
    @RequestMapping(value = "/showImage", method = { RequestMethod.POST, RequestMethod.GET })
    public ArrayList showImg() throws FileNotFoundException {
        File file = new File(imagesPath);
        File [] files = file.listFiles();
        ArrayList list = new ArrayList();
        for (int i = 0; i < files.length; i++)
        {
            File file1 = files[i];
            String name = file1.getName();//根据后缀判断
            System.out.println(name);
            list.add("111.230.252.52/vuetest/images/"+name);

        }

        System.out.println(list);
        return list;
    }


    /**
     * 这里写一个省市区三级联动
     * 这里获取的是所有省的数据
     */
    @RequestMapping(value = "/queryProvinces", method = { RequestMethod.POST, RequestMethod.GET })
    public List<SysProvinces> queryProvinces(){
        List<SysProvinces> list = queryPlaceService.queryProvince();
        return list;
    }

    /**
     * 通过省的id查询到所有的城市
     * @param sysProvinces
     * @return
     */
    @RequestMapping(value = "/queryCities",method = { RequestMethod.POST, RequestMethod.GET })
    public List<SysCities> queryCities(  @RequestBody SysProvinces sysProvinces){
        List<SysCities> list = queryPlaceService.queryCity(sysProvinces);
        return list;

    }

    /**
     * 通过市的id查询所有的区
     * @param sysCities
     * @return
     */
    @RequestMapping(value = "/queryAreas",method = { RequestMethod.POST, RequestMethod.GET })
    public List<SysAreas> queryAreas(@RequestBody SysCities sysCities){
        List<SysAreas> list = queryPlaceService.queryArea(sysCities);
        return list;
    }
}